﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.WebComponent
Imports AugmentLib.InputOutput.Encryption
Partial Class NewsList
    Inherits System.Web.UI.Page


#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 25
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadUpload()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

#End Region

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTitle.Click
        If lnkTitle.Text = "Title ▼" Then
            lnkTitle.Text = "Title ▲"
        Else
            lnkTitle.Text = "Title ▼"
        End If

        lblhidSortType.Text = " ORDER BY Title " & SortType(lnkTitle.Text)


        btnSearch_Click(sender, e)
    End Sub


    Protected Sub lnkSource_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSource.Click
        If lnkSource.Text = "Source ▼" Then
            lnkSource.Text = "Source ▲"
        Else
            lnkSource.Text = "Source ▼"
        End If

        lblhidSortType.Text = " ORDER BY Source " & SortType(lnkSource.Text)


        btnSearch_Click(sender, e)
    End Sub


    Protected Sub lnkDetails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDetails.Click
        If lnkDetails.Text = "Details ▼" Then
            lnkDetails.Text = "Details ▲"
        Else
            lnkDetails.Text = "Details ▼"
        End If

        lblhidSortType.Text = " ORDER BY Details " & SortType(lnkDetails.Text)


        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDate.Click
        If lnkDate.Text = "Date ▼" Then
            lnkDate.Text = "Date ▲"
        Else
            lnkDate.Text = "Date ▼"
        End If

        lblhidSortType.Text = " ORDER BY NewsDateTime " & SortType(lnkDate.Text)


        btnSearch_Click(sender, e)
    End Sub

    Private Sub LoadUpload()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        divError.Visible = False
        Try
            objDB.OpenConnection()

            Dim Title, Source, Details, DateMin, DateMax As String

            If txtTitle.Text <> String.Empty Then
                Title = " AND TItle LIKE @TItle"
                objDB.AddParameter("TItle", Data.SqlDbType.NVarChar, "%" & txtTitle.Text.Trim & "%")
            Else
                Title = String.Empty
            End If

            If txtSource.Text <> String.Empty Then
                Source = " AND Source LIKE @Source"

                objDB.AddParameter("Source", Data.SqlDbType.NVarChar, "%" & txtSource.Text.Trim & "%")
            Else
                Source = String.Empty
            End If

            If txtDetails.Text <> String.Empty Then
                Details = " AND Details LIKE @Details"
                objDB.AddParameter("Details", Data.SqlDbType.NVarChar, "%" & txtDetails.Text.Trim & "%")
            Else
                Details = String.Empty
            End If

            If txtDateMin.Text.Trim <> String.Empty Then
                DateMin = " AND NewsDateTime >= @DateMin"
                objDB.AddParameter("DateMin", Data.SqlDbType.DateTime, txtDateMin.Text.Trim)
            Else
                DateMin = String.Empty
            End If

            If txtDateMax.Text.Trim <> String.Empty Then
                DateMax = " AND NewsDateTime <= @DateMax"
                objDB.AddParameter("DateMax", Data.SqlDbType.Date, txtDateMax.Text.Trim)
            Else
                DateMax = String.Empty
            End If

            objDB.dbVariable.SQLString = "SELECT NewsId, Title, Source, Details, NewsDateTime" & _
                                        " FROM Mobile_News " & _
                                        "WHERE StatusDelete <> 1 " &
                                        Title & Source & Details & DateMin & DateMax &
            lblhidSortType.Text

            objDB.FillData(TempDT, "Mobile_UsefulInfo", Database.SQLCommandType.NormalString)

            objDB.CloseConnection()

            'Creating a Paged Datasouce
            Dim objPaged As New PagedDataSource
            objPaged.DataSource = TempDT.DefaultView
            objPaged.AllowPaging = True
            objPaged.PageSize = PageSize
            objPaged.CurrentPageIndex = CurrentPage

            lblHeadNoOfItem.Text = TempDT.Rows.Count
            lblFootNoOfItem.Text = TempDT.Rows.Count

            repUpload.DataSource = objPaged
            repUpload.DataBind()

            lblMaxPageCount.Text = objPaged.PageCount - 1
            lblTotalPage.Text = " of " & objPaged.PageCount

        Catch ex As Exception
            divError.Visible = True
            lblMsgError.Text = ex.Message.ToString
        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
    End Sub


    Protected Sub repUpload_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repUpload.ItemCommand
        If e.CommandName = "ViewList" Then
            Session.Add("Back", "NewsList.aspx")
            'Response.Redirect("AuctionEntry.aspx?id=" & SymmetricEncryptToHex(e.CommandArgument, "augment"))
            Response.Redirect("NewsEntry.aspx?id=" & e.CommandArgument)
        End If

        If e.CommandName.ToUpper = "DELETEITEM" Then

            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE Mobile_News SET StatusDelete=1 WHERE NewsId=@NewsId"
                objDB.AddParameter("NewsId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repUpload.Items(e.CommandArgument).FindControl("lblProjectId"), Label).Text))
                objDB.ExecuteQuery()
                objDB.CloseConnection()
                lblMsgSuccessful.Text = "Entry Deleted"
                lblMsgSuccessful.Visible = True
                LoadUpload()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")

            If arr(0).ToString.Contains("NewLaunch.aspx") Then

                Session.Remove("Back")
                LoadUpload()

            Else
                Session.Remove("Back")
            End If
        Else
            If Not Page.IsPostBack Then

                lblStaffUID.Text = Session.Item("StaffId").ToString
                lblStaffType.Text = Session.Item("StaffType").ToString
                lnkDate_Click(sender, e)
            End If
        End If

        If Not Page.IsPostBack Then

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        Session("anchor1") = ""
        LoadUpload()
    End Sub


    Protected Sub btnResetSearchFilter_Click(sender As Object, e As EventArgs) Handles btnResetSearchFilter.Click
        txtDetails.Text = ""
        txtSource.Text = ""
        txtTitle.Text = ""
        txtDateMax.Text = ""
        txtDateMin.Text = ""


    End Sub
End Class
